package org.jeecg.modules.outOrderTaskMain.service.impl;

import org.jeecg.modules.outOrderTaskMain.entity.OutOrderTaskMain;
import org.jeecg.modules.outOrderTaskMain.entity.OutOrderTaskDetail;
import org.jeecg.modules.outOrderTaskMain.mapper.OutOrderTaskDetailMapper;
import org.jeecg.modules.outOrderTaskMain.mapper.OutOrderTaskMainMapper;
import org.jeecg.modules.outOrderTaskMain.service.IOutOrderTaskMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 出库任务
 * @Author: jeecg-boot
 * @Date:   2024-10-10
 * @Version: V1.0
 */
@Service
public class OutOrderTaskMainServiceImpl extends ServiceImpl<OutOrderTaskMainMapper, OutOrderTaskMain> implements IOutOrderTaskMainService {

	@Autowired
	private OutOrderTaskMainMapper outOrderTaskMainMapper;
	@Autowired
	private OutOrderTaskDetailMapper outOrderTaskDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(OutOrderTaskMain outOrderTaskMain, List<OutOrderTaskDetail> outOrderTaskDetailList) {
		outOrderTaskMainMapper.insert(outOrderTaskMain);
		if(outOrderTaskDetailList!=null && outOrderTaskDetailList.size()>0) {
			for(OutOrderTaskDetail entity:outOrderTaskDetailList) {
				//外键设置
				entity.setOutOrderTaskId(outOrderTaskMain.getId());
				outOrderTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(OutOrderTaskMain outOrderTaskMain,List<OutOrderTaskDetail> outOrderTaskDetailList) {
		outOrderTaskMainMapper.updateById(outOrderTaskMain);
		
		//1.先删除子表数据
		outOrderTaskDetailMapper.deleteByMainId(outOrderTaskMain.getId());
		
		//2.子表数据重新插入
		if(outOrderTaskDetailList!=null && outOrderTaskDetailList.size()>0) {
			for(OutOrderTaskDetail entity:outOrderTaskDetailList) {
				//外键设置
				entity.setOutOrderTaskId(outOrderTaskMain.getId());
				outOrderTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		outOrderTaskDetailMapper.deleteByMainId(id);
		outOrderTaskMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			outOrderTaskDetailMapper.deleteByMainId(id.toString());
			outOrderTaskMainMapper.deleteById(id);
		}
	}
	
}
